package com.qeebu.SMSManagerClient.smsList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qeebu.SMSManagerClient.base.BaseActivity;
import com.qeebu.SMSManagerClient.R;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 程序主入口activity
 * 用于显示所有的短信对话列表，避免首次加载所有短信过慢的问题
 * 
 * @author yongjun
 * 
 */
public class ConversionSMSListActivity extends BaseActivity implements
        AdapterView.OnItemClickListener
{
    private ListView sms_dialog_list;
    
    private SMSListAdapter dialogAdapter;

    private View progressBar;

    Button contacts;

    Button exit;

    private List<ConversionSMSItem> smsList = new ArrayList<ConversionSMSItem>();

    public List<ConversionSMSItem> getSmsList()
    {
        return smsList;
    }

    public void setSmsList(List<ConversionSMSItem> smsList)
    {
        this.smsList = smsList;
    }

    public View getProgressBar()
    {
        return progressBar;
    }

    public void setProgressBar(View progressBar)
    {
        this.progressBar = progressBar;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.sms_list_dialog_layout);
        sms_dialog_list = (ListView) findViewById(R.id.sms_list);
        progressBar = findViewById(R.id.smslist_progress_bar);

        dialogAdapter = new SMSListAdapter(ConversionSMSListActivity.this); // 设置短信列表的Adapter，监听者
        sms_dialog_list.setAdapter(dialogAdapter);
        sms_dialog_list.setOnItemClickListener(this);

        ReadConversionSMSAsyncTask readTask = new ReadConversionSMSAsyncTask(this); // 用异步线程读取短信对话
        readTask.execute(null);

        progressBar.setVisibility(View.VISIBLE);              // 显示加载进度条
    }

    public void setAdapter()
    {
        sms_dialog_list.setAdapter(dialogAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        return true;
    }

    class SMSListViewHold
    {
        public TextView phoneNum;
        public TextView smsCount;
        public TextView smsDate;
        public TextView smsBody;
    }

    class SMSListAdapter extends BaseAdapter
    {
        private Context context;

        public SMSListAdapter(Context ctx)
        {
            context = ctx;
        }
           
        /**
         * 获取viewlist的每一行view
         */
        public View getView(int position, View convertView, ViewGroup parent)
        {
            SMSListViewHold viewHold;

            if (convertView == null)
            {
                LayoutInflater inflater = LayoutInflater.from(context);
                convertView = inflater.inflate(R.layout.sms_list_dialog_item,
                        null);
                viewHold = new SMSListViewHold();
                viewHold.phoneNum = (TextView) convertView.findViewById(R.id.sms_phonenum);
                viewHold.smsBody = (TextView) convertView.findViewById(R.id.sms_body);
                viewHold.smsCount = (TextView) convertView.findViewById(R.id.sms_count);
                viewHold.smsDate = (TextView) convertView.findViewById(R.id.sms_date);
                convertView.setTag(viewHold);
            }
            else
            {
                viewHold = (SMSListViewHold) convertView.getTag();
            }
            viewHold.phoneNum.setText(smsList.get(position).getPhoneNum());
            viewHold.smsBody.setText(smsList.get(position).getBody());
            viewHold.smsCount.setText("共" + smsList.get(position).getCount() + "条");
            viewHold.smsDate.setText(smsList.get(position).getDate());
            // convertView.setTag(viewHold);
            return convertView;
        }

        public int getCount()
        {
            if (smsList == null || smsList.isEmpty())
            {
                return 0;
            }
            return smsList.size();
        }

        public Object getItem(int position)
        {
            try
            {
                return smsList.get(position);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return null;
            }
        }

        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return position;
        }
    }

    public void onItemClick(AdapterView<?> l, View v, int position, long id)
    {
        Intent intent = new Intent();
        intent.setClass(ConversionSMSListActivity.this, PersonSMSListActivity.class);
        Bundle data = new Bundle();
        data.putLong("id", smsList.get(position).getId());
        data.putString("name", smsList.get(position).getPhoneNum());
        intent.putExtras(data);
        startActivity(intent);
    }

}
